package com.zx.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class NgnixDateUtil {
	
	public static final int	 DEFAULT_DATE_FORMAT_INDEX 		= 0X0;
	
	public static final int  PREPARE_DATE_FORMAT_INDEX_1 	= 0X1;

	public static final int  PREPARE_DATE_FORMAT_INDEX_2 	= 0X2;
	
	public static final int  PREPARE_DATE_FORMAT_INDEX_3	= 0X3;
	
	private static SimpleDateFormat defaultDateFormat; 

	private static SimpleDateFormat prepareDateFormat1; 

	private static SimpleDateFormat prepareDateFormat2; 

	private static SimpleDateFormat prepareDateFormat3; 
	
	private static final RuntimeException TYPE_NOT_SUPPORT = new RuntimeException("时间类型不支持");
	
	static {
		Locale usLocale = new Locale("en","US","");
		defaultDateFormat = new SimpleDateFormat("d/MMM/yyyy:HH:mm:ss Z", usLocale);
		prepareDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		prepareDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
		prepareDateFormat3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	}
	
	/**
	 * 类型装换
	 * @param dateStr
	 * @param formatIndex
	 * @return
	 * @author: xue.zhang
	 * @date 2018年8月1日下午8:18:52
	 */
	public static Date parse(String dateStr, int formatIndex) {
		try {
			switch (formatIndex) {
			case DEFAULT_DATE_FORMAT_INDEX:
				return defaultDateFormat.parse(dateStr);
			case PREPARE_DATE_FORMAT_INDEX_1:
				return  prepareDateFormat1.parse(dateStr);
			case PREPARE_DATE_FORMAT_INDEX_2:
				return  prepareDateFormat2.parse(dateStr);
			case PREPARE_DATE_FORMAT_INDEX_3:
				return  prepareDateFormat3.parse(dateStr);
			default:
				throw TYPE_NOT_SUPPORT;
			}
		} catch (ParseException e) {
			throw TYPE_NOT_SUPPORT;
		}
	}
	
	/**
	 * 获得时间的类型
	 * @param dateStr
	 * @return
	 * @author: xue.zhang
	 * @date 2018年8月1日下午8:22:34
	 */
	public static int getFormatIndex(String dateStr) {
		try {
			defaultDateFormat.parse(dateStr);
			return DEFAULT_DATE_FORMAT_INDEX;
		} catch (ParseException e) { }
		
		try {
			prepareDateFormat1.parse(dateStr);
			return PREPARE_DATE_FORMAT_INDEX_1;
		} catch (ParseException e) { }
		
		try {
			prepareDateFormat2.parse(dateStr);
			return PREPARE_DATE_FORMAT_INDEX_2;
		} catch (ParseException e) { }
		
		try {
			prepareDateFormat3.parse(dateStr);
			return PREPARE_DATE_FORMAT_INDEX_3;
		} catch (ParseException e) { }
		throw TYPE_NOT_SUPPORT;
	}
	
}
